#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：turbo 
@File    ：serializers.py
@Author  ：赵加响
@Date    ：2023/4/23 10:43 
@Desc    ：
"""
from rest_framework import serializers

from apps.rbac.models import Role, Menu
from constants import LANGUAGE_ZH


class RoleDropdownSerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()

    class Meta:
        model = Role
        fields = ('id', 'code', 'name')

    def get_name(self, obj):
        request = self.context['request']
        localization = getattr(request, 'LANGUAGE_CODE', LANGUAGE_ZH)

        if localization == LANGUAGE_ZH:
            return obj.name_zh
        else:
            return obj.name_en


class MenuDropdownSerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()

    class Meta:
        model = Menu
        fields = ('id', 'code', 'name')

    def get_name(self, obj):
        request = self.context['request']
        localization = getattr(request, 'LANGUAGE_CODE', LANGUAGE_ZH)

        if localization == LANGUAGE_ZH:
            return obj.name
        else:
            return obj.name_en
